package jdbclearn1;/*
@Date:2025/3/11
*/

import java.sql.*;
import java.util.Scanner;

public class JdbcStudy01 {
    private String url = "jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC";
    private String user = "root";
    private String password = "123456";

    public JdbcStudy01() {
    }

    public JdbcStudy01(String url, String user, String password) {
        this.url = url;
        this.user = user;
        this.password = password;
    }

    public boolean isAdmin(String admin, String pwd) throws Exception {
        Connection conn = conn();
        Statement statement = conn.createStatement();
        String sql = "select * from manager where admin='" + admin + "' and password='" + pwd + "'";
        ResultSet resultSet = statement.executeQuery(sql);
        return resultSet.next();
    }

    public Connection conn() throws Exception {
        Connection conn = null;
        //注册操作数据的驱动 反射
        Class.forName("com.mysql.cj.jdbc.Driver");
        //使用api链接数据库
        conn = DriverManager.getConnection(url, user, password);
        System.out.println("conn = " + conn);
        return conn;
    }

    public static int stateObj(Connection conn, String sql) throws SQLException {
        //获取查询对象
        Statement statement = conn.createStatement();
        System.out.println("statement = " + statement);
        //使用查询对象执行sql
        int i = statement.executeUpdate(sql);
        System.out.println("i = " + i);
        return i;
    }

    //数据查询
    public void findScore(String sql) throws Exception {
        Connection conn = conn();
        //获取查询对象
        Statement statement = conn.createStatement();
        System.out.println("statement = " + statement);
        //使用查询对象执行sql
        ResultSet resultSet = statement.executeQuery(sql);
        System.out.println("resultSet = " + resultSet);
        //解析数据
        while ((resultSet.next())) {
            int score_id = resultSet.getInt("score_id");
            System.out.println(score_id);
        }
    }

    //实现数据入库
    public void insertScore(String sql) throws Exception {
        Connection conn = conn();
        int i = stateObj(conn, sql);
        System.out.println("i = " + i);
    }

}

class test {
    public static void main(String[] args) throws Exception {
        System.out.println("请输入账户名");
        String admin = new Scanner(System.in).next();
        System.out.println("请输入密码");
        String password = new Scanner(System.in).next();
        final JdbcStudy01 jdbcStudy01 = new JdbcStudy01();
        System.out.println(jdbcStudy01.isAdmin(admin, password));

    }
}